import  { useEffect, useState } from "react";

// 创建自定义hooks
function useDebounce(value: any, delay = 300) {
    const [debouncedValue, setDebouncedValue] = useState(value);
    useEffect(() => {
        const handler = setTimeout(() => {
            setDebouncedValue(value)
        }, delay)
        // 清除定时器，会在下次update的时候执行这段清除
        return () => {
            clearTimeout(handler);
        }
    }, [value, delay]);
    return debouncedValue;
}
export default useDebounce;